<?php

function get_favorite_list($con) {
	$query = "SELECT * FROM user_favorite ORDER BY id DESC";
	return select($con, $query);
}

function insert_favorite($con, $paradise_id, $user_id) {

	$query = "INSERT INTO user_favorite(user_id, paradise_id) " . "VALUES('$user_id', '$paradise_id')";
	error_log($query);
	return insert($con, $query);
}

function getFavorite($con, $user_id, $paradise_id) {
	$query = "SELECT * FROM user_favorite where user_id=$user_id and paradise_id=$paradise_id";
	return select($con, $query);
}

function delete_favorite($con, $paradise_id, $user_id) {
	$query = "delete from user_favorite where user_id=$user_id and paradise_id=$paradise_id";
	delete($con, $query);
}

function add_remove_favorite($paradise_id) {
	session_start();
	if (isset($_SESSION['user_id'])) {
		if (getFavorite($con, $_SESSION['user_id'], $paradise_id)) {
			insert_favorite($con, $paradise_id, $_SESSION['user_id']);
		} else {
			delete_favorite($con, $paradise_id, $_SESSION['user_id']);
		}
	} else {
		return false;
	}
}

function get_favorite_tags($con){
	session_start();
	$user_id=$_SESSION['user_id'];
	$query="select distinct tag.name from tag, user_favorite, paradise where tag.paradise_id=paradise.id and paradise.id=user_favorite.paradise_id and user_favorite.user_id=$user_id";
	return select($con,$query);
	}

function is_favorite($paradise_id) {
	$con = get_db_con();
	$user_id = $_SESSION['user_id'];
	
	if (getFavorite($con, $user_id, $paradise_id)) {
		return true;
	}
	return false;
}
